print_title <- function(index, title_str) {
  print(paste0(">>>", index, ", ", title_str, "------------"))
}

# create
print_title(1, "create factor")
x <- c("优", "中", "良", "优", "良", "良")
x1 <- factor(x, levels=c("中", "良", "优"))

print_title(2, "sort factor by levels")
print(sort(x1))


# as number
print_title(3, "as numeric")
as.numeric(x1)

# draw chart
table(x1)
print_title(4, "draw chart")
library("ggplot2")
library("tidyverse")
# ggplot(tibble(x1), aes(x1)) + geom_bar()


# access element
print_title(5, "modify factor")
levels(x1) <- c("Excellent", "Good", "Fair")
print(x1)



# replace element
print_title(6, "create order factor")
x2 <- factor(x, levels = c("中", "良", "优"), ordered=TRUE)
print(x2)


# function cut
print_title(7, "function cut")
Age <- c(25, 15, 36, 57, 74, 29)
cut(Age, 
    breaks = c(0, 18, 45, 100),
    lables = c("Young", "Middle", "Old"))


# tidyverse